package com.ljkj.cordial.chat.util;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;

/**
 * 提交按钮状态工具类
 */
public abstract class ButtonSubmitStateUtil implements TextWatcher {
    private TextView tvSubmit;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        setButtonSummitState(tvSubmit, checkState());
    }

//    private static class Build {
//        public static ButtonSubmitStateUtil INSTANCE = new ButtonSubmitStateUtil();
//    }
//
//    public static ButtonSubmitStateUtil getInstance() {
//        return Build.INSTANCE;
//    }


    public ButtonSubmitStateUtil(TextView tvSubmit, EditText... editTexts) {
        this.tvSubmit = tvSubmit;
        this.tvSubmit.setEnabled(false);
        setButtonSummitState(tvSubmit, checkState());
        for (int i = 0; i < editTexts.length; i++) {
            editTexts[i].addTextChangedListener(this);
        }
    }

    private void setButtonSummitState(TextView textView, boolean isEnable) {
        if (isEnable) {
            textView.setEnabled(true);
            textView.setTextAppearance(textView.getContext(), R.style.ButtonGreenStyle);
            textView.setBackgroundResource(R.drawable.btn_green);
        } else {
            textView.setEnabled(false);
            textView.setTextAppearance(textView.getContext(), R.style.ButtonGrayStyle);
            textView.setBackgroundResource(R.drawable.btn_gray);
        }
    }

    /**
     * 校验输入
     *
     * @return
     */
    public abstract boolean checkState();


}
